package com.foreveredu.propms.web.struts.action;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.service.IPropQueryService;
import com.foreveredu.propms.web.struts.actionform.PropQueryActionForm;
import com.foreveredu.propms.web.util.ExportExcelUtils;

/**
 * 处理物业收费查询请求
 * 
 * @author hushigang
 * 
 */
public class QueryOweCollectChargeListAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;
	private IPropQueryService propQueryService = null;
	int distId,subId,year,month;
	// 获得日志记录器
	Logger log = Logger.getLogger(AccountAction.class);

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public void setPropQueryService(IPropQueryService propQueryService) {
		this.propQueryService = propQueryService;
	}

	public IPropQueryService getPropQueryService() {
		return propQueryService;
	}
	
	// 跳转到业主欠费明细查询条件页面
	public ActionForward queryOweCollectChargeList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		//获得所有的物业服务处
		List<District> distList = managementService.findDistricts();
		request.setAttribute("distList", distList);
		
		return mapping.findForward("query_oweCollectChargeList");
	}
	//执行业主欠费明细查询操作
	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		
		if(request.getParameter("subId")!=null){
			 distId = Integer.parseInt(request.getParameter("distId"));
			 subId = Integer.parseInt(request.getParameter("subId"));
			 year = Integer.parseInt(request.getParameter("year"));
			month = Integer.parseInt(request.getParameter("month"));
		}else{
			//获得表单数据
			PropQueryActionForm pqform = (PropQueryActionForm)form;
			 distId = pqform.getDistId();
			 subId = pqform.getSubId();
			 year = pqform.getYear();
			 month = pqform.getMonth();
		}
		
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			return mapping.findForward("doQuery_oweCollectChargeList");
		}
		//当管理员不选择物业服务处，则查询所有业主当前欠费明细
		PageModel pm = new PageModel();
		if (distId == 0) {
			//获得所有的小区所有业主欠费明细
			pm = propQueryService.findAllOwdCollectChargeList(offset, year, month);
		} else {
			pm = propQueryService.findOwdCollectChargeBySubList(offset,managementService.findSubDistrictById(subId), year, month);
		}
		request.setAttribute("pm", pm);
		request.setAttribute("distId", distId);
		request.setAttribute("subId", subId);
		request.setAttribute("year", year);
		request.setAttribute("month", month);
		
		return mapping.findForward("doQuery_oweCollectChargeList");
	}
	
	//执行业主欠费明细导出操作
	public ActionForward doExportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
			// 获得表单数据
			List<Map<String, Object>> queryList = new ArrayList<Map<String, Object>>();
			if (distId == 0) {
				//获得所有的小区所有业主欠费明细
				queryList = propQueryService.findAllOwdCollectChargeList( year, month);
			} else {
				queryList = propQueryService.findOwdCollectChargeBySubList(managementService.findSubDistrictById(subId), year, month);
			}
			try{
				response.setContentType("octets/stream"); 
				//设置表名
				String fileName="业主欠费明细表("+year+"年"+month+"月)";
				response.addHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("GBK"),"ISO8859-1")+".xls"); 
				
	            OutputStream out = response.getOutputStream();
	            
	            //设置表格中的字段名
	            String[] headers = {"小区","业主姓名", "物业服务费", "单独物业服务费","电费","水费","停车服务费","垃圾清运费","路灯电费","二次提水电费","装修垃圾清运费","装修保证金","装修服务费","单独电费","单独水费","单独垃圾清运费","单独路灯电费","单独二次提水费","欠费总额"};
	            //设置Key，然后通过这个Key去Map数组中取值
	            String[] columns ={"subName","oweName","amount4","amount5","amount6","amount7","amount8","amount9","amount10","amount11","amount12","amount13","amount14","amount15","amount16","amount17","amount18","amount19","amount20"};
	            
	            ExportExcelUtils.exportExcel("业主欠费汇总表", headers, columns, queryList, out, "");
	            out.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}	
}
